package com.example.treasure_back.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasure_back.enumerate.ExchangeType;
import com.example.treasure_back.mapper.ExchangeRecordMapper;
import com.example.treasure_back.model.ExchangeRecordModel;
import com.example.treasure_back.model.vo.ExchangeViewModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExchangeRecordService {

    @Autowired
    private ExchangeRecordMapper exchangeRecordMapper;
    @Autowired
    private GoodsService goodsService;

    public void insertExchangeRecord(String email, Integer goodsId){
        ExchangeRecordModel exchangeRecordModel = new ExchangeRecordModel();
        exchangeRecordModel.setGoodsId(goodsId);
        exchangeRecordModel.setEmail(email);
        exchangeRecordModel.setStatus(ExchangeType.NO_EXCHANGE);
        exchangeRecordMapper.insert(exchangeRecordModel);
    }

    public List<ExchangeViewModel> getExchangeRecordsByType(String email, ExchangeType status){
        QueryWrapper<ExchangeRecordModel> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        queryWrapper.eq("status", status);
        queryWrapper.eq("email", email);
        List<ExchangeRecordModel> exchangeRecordModels = exchangeRecordMapper.selectList(queryWrapper);
        ArrayList<ExchangeViewModel> exchangeViewModels = new ArrayList<>();
        for (ExchangeRecordModel exchangeRecordModel : exchangeRecordModels) {
            ExchangeViewModel exchangeViewModel = new ExchangeViewModel();
            exchangeViewModel.setId(exchangeRecordModel.getId());
            exchangeViewModel.setGetTime(f.format(exchangeRecordModel.getGetTime()));
            exchangeViewModel.setDueTime(f.format(exchangeRecordModel.getDueTime()));
            if(exchangeRecordModel.getExchangeTime()!=null){
                exchangeViewModel.setExchangeTime(f.format(exchangeRecordModel.getExchangeTime()));
            }
            exchangeViewModel.setStatus(exchangeRecordModel.getStatus());
            String name = goodsService.getGoodsNameById(exchangeRecordModel.getGoodsId());
            String image = goodsService.getGoodsImageById(exchangeRecordModel.getGoodsId());
            exchangeViewModel.setGoodsName(name);
            exchangeViewModel.setGoodsImage(image);
            exchangeViewModels.add(exchangeViewModel);
        }
        return exchangeViewModels;
    }

    public Integer getGoodsIdById(Integer id){
        ExchangeRecordModel exchangeRecordModel = exchangeRecordMapper.selectById(id);
        return exchangeRecordModel.getGoodsId();
    }

    public void changeStatus(Integer id, ExchangeType exchangeType){
        UpdateWrapper<ExchangeRecordModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("status",exchangeType);
        exchangeRecordMapper.update(new ExchangeRecordModel(),updateWrapper);
    }
}
